<?php
include_once '../header1.php';
?>
<link rel="stylesheet" href="../css/datepicker.css" type="text/css" />
    <link rel="stylesheet" media="screen" type="text/css" href="../css/layout.css" />
  	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/datepicker.js"></script>
    <script type="text/javascript" src="../js/eye.js"></script>
    <script type="text/javascript" src="../js/utils.js"></script>
    <script type="text/javascript" src="../js/layout.js?ver=1.0.2"></script>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
</script>
<script type="text/javascript">
function DeleteVerification(rownum,variables)
{
	var rownum=rownum;
	var id="Verifi";
var r=confirm("Are you sure to delete this?");
if(r==true)
{
document.getElementById(id).value=variables;
}
}
</script>
<script type="text/javascript">
function GetTBox(tbox,rows)
{
	var i;
	var box="groupcolumn"+tbox;
	for(i=1;i<=rows;i++)
	{
		var tb="groupcolumn"+i;
		var b="button"+i;
		if(tb!=box)
		{
		document.getElementById(tb).value="";
		document.getElementById(b).innerHTML="";
		}
	}
	
}
</script>
<script type="text/javascript">
function GetCol(val,name,rownum)
{
	var rownum=rownum;
	var xmlhttp; 
var b="button"+rownum;
var n=val+","+rownum+","+name;
if (val=="")
  {
  document.getElementById(b).innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById(b).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","butt.php?q="+n,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php

include_once ("../Login/databaseconnection.php");
if(!loggedin())
{
require_once ("../index.php");
}   
   else
   {
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
   $action="";
    if(!isset($_POST['gby'])&&!isset($_POST['groupback']))
  {
	   $_SESSION['sele_groupby']="";
	   $_SESSION['back_groupby']=0;	
 $_SESSION['Column_name']="";	
$_SESSION['gvals']=""; 
}	   
	   $action="";
  if(isset($_POST['gby']))
  {
	  $action="group";
	 $crvalues=$_POST['crval'];
	 $crvalu=explode(",",$crvalues);
	 $Column=$crvalu[0];
	 $rownum=$crvalu[1];
    $pri_keys=$_POST['pri'];
	 $prikeys=explode(",",$pri_keys);
	$Competition_Id=$prikeys[0];
	$Date=$prikeys[1];
	$School_Id=$prikeys[2];
    	 if(empty($_SESSION['sele_groupby']))
	  {
		 $_SESSION['sele_groupby']=$pri_keys.",".$Column;
	  }
	  else
	  {
		 $_SESSION['sele_groupby']=$_SESSION['sele_groupby']."_".$pri_keys.",".$Column;
	  } 
$sele_groupby=explode("_",$_SESSION['sele_groupby']);
if($Column==1)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Competition_Id";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Competition_Id";
}
}
if($Column==2)
{
if(empty( $_SESSION['Column_name']))
{
$_SESSION['Column_name']="School_Id";
}
else
{
$_SESSION['Column_name']= $_SESSION['Column_name'].",School_Id";
}
}
if($Column==3)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="No_Of_Students";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",No_Of_Students";
}
}
if($Column==4)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="No_Of_Teachers";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",No_Of_Teachers";
}
}
 if($Column==5)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Result";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Result";
}
}
if($Column==6)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Date";
}
else
{
$_SESSION['Column_name']= $_SESSION['Column_name'].",Date";
}
}
if($Column==7)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Scope";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Scope";
}
}
if($Column==8)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Winner";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Winner";
}
}
if($Column==9)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Last_Updated_Date";
}
else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Last_Updated_Date";
}
}
if($Column==10)
{
if(empty( $_SESSION['Column_name']))
{
 $_SESSION['Column_name']="Competition_Id";
}

else
{
 $_SESSION['Column_name']= $_SESSION['Column_name'].",Competition_Id";
}
}

  $Column_names=explode(",", $_SESSION['Column_name']);
  $tot_cols=count($Column_names);
  $Exc_que="SELECT * FROM competition WHERE Competition_Id='".$Competition_Id."' AND Date='".$Date."' AND School_Id='".$School_Id."'";
   $gro=mysql_query("SELECT * FROM competition WHERE Competition_Id='".$Competition_Id."' AND Date='".$Date."' AND School_Id='".$School_Id."'");
     $number_of_rows=mysql_num_rows($gro);
if($rowgro=mysql_fetch_array($gro))
{
$Col_name=$Column_names[$tot_cols-1];
if($rowgro[$Col_name]=="")
{
$_SESSION['value_empty']="Sorry,the column value is empty";
}
else
{
if(empty($_SESSION['gvals']))
{
$_SESSION['gvals']=$rowgro[$Col_name];
}
else
{
$_SESSION['gvals']=$_SESSION['gvals'].",".$rowgro[$Col_name];
}
}
}
	}  
   if(isset($_POST['groupback']))
  {
	  $sele_groupby=explode("_",$_SESSION['sele_groupby']);
	 $_SESSION['back_groupby']=$_SESSION['back_groupby']+1;
	
	 $gval=explode(",",$_SESSION['gvals']);
  $Column_names=explode(",",$_SESSION['Column_name']);
  $Columns="";
 $Vals="";
  for($b=0;$b<count($Column_names)-1;$b++)
  { 
   if(empty($Vals))
  {
  $Vals=$gval[$b];
  }
  else
  {
  $Vals=$Vals.",".$gval[$b];
  }
   if(empty($Columns))
  {
  $Columns=$Column_names[$b];
  }
  else
  {
  $Columns=$Columns.",".$Column_names[$b];
  }
  }
  $_SESSION['gvals']=$Vals;
$_SESSION['Column_name']=$Columns;
  $gval=explode(",",$_SESSION['gvals']);
  $Column_names=explode(",",$_SESSION['Column_name']);
    if($_SESSION['Access_Type']=="Administrator")
  {
    if($_SESSION['Column_name']=="")
  {
   $res_que="SELECT * FROM competition";
     }
  else
  {
   $res_que="SELECT * FROM competition WHERE ";
     for($j=0;$j<count($Column_names);$j++)
  {
  $Col_name=$Column_names[$j];
     $res_que=$res_que.$Col_name."='".$gval[$j]."'";
   if($j!=(count($Column_names)-1))
  {
  $res_que=$res_que." AND ";
  }
  }
  }
  $Exc_que=$res_que;
    $resultgro=mysql_query($res_que);
   $number_of_rows=mysql_num_rows($resultgro);
    }
 else if($_SESSION['Access_Type']=="Teacher in Charge"||$_SESSION['Access_Type']=="Teacher")
  {
  $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   }
  if($_SESSION['Column_name']=="")
  {
   $res_que="SELECT * FROM competition WHERE School_Id='".$Own_School_Id."'";
     }
  else
  {
   $res_que="SELECT * FROM competition WHERE ";
    for($j=0;$j<count($Column_names);$j++)
  {
  $Col_name=$Column_names[$j];
      $res_que=$res_que.$Col_name."='".$gval[$j]."'";
   if($j!=(count($Column_names)-1))
  {
  $res_que=$res_que." AND ";
  }
  if($j==(count($Column_names)-1))
  {
  $res_que=$res_que." AND School_Id='".$Own_School_Id."'";
  }
  }
  }
     $resultgro=mysql_query($res_que);
	 $Exc_que=$res_que;
   $number_of_rows=mysql_num_rows($resultgro);
     }
  }
    if(isset($_POST['dele']))
  {
   $action="delete";
   }
   ?>
<?php
include_once "../header2.php";
?>
<?php
include_once "topmenu.php";
?>
<div id="content">
	<div id="main">
	  <div id="bodyfield">
<br/>
<?php
if($action=="delete")
{
$dvari=$_POST['Verifi'];
if($dvari!="")
{
$vari=explode(",",$dvari);
$Competition_Id=$vari['0'];
$Date=$vari['1'];
$School_Id=$vari['2'];
}
if($dvari!="")
{
if($_SESSION['Access_Type']=="Administrator")
{
$resultdel=mysql_query("DELETE FROM competition WHERE Competition_Id='".$Competition_Id."' AND Date='".$Date."' AND School_Id='".$School_Id."'");
if(mysql_affected_rows()!=0)
{
$_SESSION['del']="Successfully deleted!";
}
if(mysql_affected_rows()==0)
{
$_SESSION['del']="Sorry unable to delete this!";
}
}
else if($_SESSION['Access_Type']=="Teacher in Charge")
{
$result=mysql_query("SELECT * FROM competition WHERE Competition_Id='".$Competition_Id."' AND Date='".$Date."' AND School_Id='".$School_Id."'");
	$Accessed_By=$_SESSION['NIC_Number'];
	if($row=mysql_fetch_array($result))
   {
   $Competition_Id=$row['Competition_Id'];
   $School_Id=$row['School_Id'];
   $No_Of_Students=$row['No_Of_Students'];
   $No_Of_Teachers=$row['No_Of_Teachers'];
   $Result=$row['Result'];
   $Date=$row['Date'];
   $Scope=$row['Scope'];
   $Winner=$row['Winner']; 
$Last_Updated_Date=$row['Last_Updated_Date'];   
   }
   	 $sql=mysql_query("INSERT INTO deleted_competition(Competition_Id,School_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Deleted_date,Accessed_By,Last_Updated_Date)values('$Competition_Id','$School_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql){
$del=mysql_query("DELETE FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['del']="This record is successfully deleted!";
 
  }
  if(mysql_affected_rows()==0)
  {
 $_SESSION['del']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_competition WHERE Competition_Id='$Competition_Id' AND Date='$Date' AND School_Id='$School_Id' AND Deleted_Date='".$t_d."'");
  }
  }
    if(!$sql)
{
 $_SESSION['del']="Sorry unable to delete this record!";
 }
}
}
}
echo '<form  action="#" method="POST">';
echo "<input type='hidden' size='1' id='Verifi' name='Verifi'/>";
echo '<br/>';
 ?>
<font class="err">
<?php
if($_GET['action']=="edit"&&$action!="delete"&&$action!="group"&&!isset($_POST['groupback'])&&$_SESSION['Access_Type']!="Teacher")
	{	
$Competition_Id=$_GET['Competition_Id'];
$Date=$_GET['Date'];
$School_Id=$_GET['School_Id'];
$result=mysql_query("SELECT * FROM competition WHERE Competition_Id='".$Competition_Id."' AND Date='".$Date."' AND School_Id='".$School_Id."'");
if($result)
   {
$qucol="SHOW COLUMNS FROM competition";
$resultcol=mysql_query($qucol);
$totcol=mysql_num_rows($resultcol);
while($rowcolu=mysql_fetch_row($resultcol))
{
if($rowcolu[0]!="Last_Updated_Date")
{
if(empty($colnamearray))
{
if($Col_name=="School_Id")
{
$colnamearray='School_Name';
}
else
{
$colnamearray=$rowcolu[0];
}
}
else
{
if($Col_name=="School_Id")
{
$colnamearray=$colnamearray.",".'School_Name';
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
}
}
}
$colnamearray=$colnamearray.",Last_Updated_Date";
$colnamearray=$colnamearray.",Competition Name";

	$cnamearray=explode(",",$colnamearray);
    if(!isset($_POST['Go']))
	 {
	 echo '<table class="tablefontv">';
	if($row=mysql_fetch_array($result))
  {
 $cnamearray=explode(',',$colnamearray);
    for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	
	  $th_cname= str_replace("_"," ",$val);
	 $valu=$row[$val];
		if($val!="Last_Updated_Date"&&$val!="Competition Name")
		{
    echo '<tr>';
        echo '<th width="200" height="29" scope="row" bgcolor="#3399CC">';
		echo $th_cname.":";
		echo '</th>';
        echo '<td width="144">';
		if($th_cname=="Date")
		{
		if(isset($valu))
		{
		echo "<input type='text' name='$val' value='$valu' class='inputDate' id='inputDate'>";
		}
		else
		{
		echo "<input type='text' name='$val' value='$t_d' class='inputDate' id='inputDate'>";
		}
		}
		else
		{
		echo "<input type='text' name='$val' value='$valu'>";
		}
		echo '</td>';
      echo '</tr>';
	  }
	}
}
	  echo '</table>';
	  echo '<br/>';
	   echo '<input name="Go" align="middle" type="submit" value="Go" class="gobutton"/>';	
	   }
	   if(isset($_POST['Go']))
	   {
	$Competition_Id1=$_POST['Competition_Id'];
	$School_Id1=$_POST['School_Id'];
    $No_Of_Students=$_POST['No_Of_Students'];
    $No_Of_Teachers=$_POST['No_Of_Teachers'];
    $Result=$_POST['Result'];
    $Date1=$_POST['Date'];
    $Scope=$_POST['Scope'];
   $Winner=$_POST['Winner'];
  date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	
$up=mysql_query("UPDATE competition SET Competition_Id='$Competition_Id1',School_Id='$School_Id1',No_Of_Students='$No_Of_Students',No_Of_Teachers='$No_Of_Teachers',Result='$Result',Date='$Date1',Scope='$Scope',Winner='$Winner',Last_Updated_Date='$t_d' WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
if(!$up)
	   {
	   $_SESSION['up']="Sorry unable to edit this please try again!";
	   }
	             	   if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	$T_NIC_Number=$_GET['T_NIC_Number'];
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   $Exc_que="SELECT * FROM competition WHERE School_Id='$Own_School_Id'";
  $result=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   $number_of_rows=mysql_num_rows($result);
  }
  }
	else if($_SESSION['Access_Type']=="Administrator")
	{
	 $result=mysql_query("SELECT * FROM competition");
   $Exc_que="SELECT * FROM competition";
   $number_of_rows=mysql_num_rows($result);
	 }
if(mysql_num_rows($result)!=0)
   {
   $rnum=0;
   if(mysql_num_rows($result)>=10)
   {
	 echo '<div style="height:410px; overflow-Y:scroll; ">';
	 }
  echo '<table width="937" border="1" class="tablefontv">';
  	if($result)
  {
echo '<tr bgcolor="#6AB5FF">';
		if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
echo '<td width="39"></td>
<td width="39"></td>';
}
$cnamearray=explode(',',$colnamearray);
    for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	 $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
         echo '<td>';
		if($val=="School_Id")
  {
    $cnu=$cv+1;
		echo "(".$cnu.")School Name";
  }
  else
  {
		  $cnu=$cv+1;
		echo "(".$cnu.")".$th_cname;
		}
		echo '</td>';
    }
		echo '<td width="159">Column No</td>';
	echo '</tr>';
}

  while($row=mysql_fetch_array($result))
  {
	  $rnum=$rnum+1;
$c=$row['Competition_Id'];
$d=$row['Date'];
$s=$row['School_Id'];
$delVerification="delVerification".$rnum;
$name=$c.",".$d.",".$s;
?>
<tr bgcolor="#999999"><form action="#" method="post"><?php
   	if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	?>
<td width="39">
<a href='?action=edit&Competition_Id=<?php echo $c; ?>&Date=<?php echo $d; ?>&School_Id=<?php echo $s; ?>'><img src="../images/edit.png"/></a></td><td width="39"><?php echo "<input type='submit' id='$name' name='dele' onclick='DeleteVerification($rnum,this.id)' class='submitdelete' value=''/>";?></td>
	<?php
	}
$cnamearray=explode(',',$colnamearray);
	for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
    if($cv==9)
	{
	$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$C_ID'");
  if($rownet=mysql_fetch_array($resultnet))
  {

    echo '<td>';
		echo $rownet['Competition_Name'];
		echo '</td>';
 }
  else
  {
  
    echo '<td>';
		echo '';
		echo '</td>';
}
}
else
{
if($val=="School_Id")
{
$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$valu."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
echo '<td>';
		echo $Name;
		echo '</td>';
}
else
{
        echo '<td>';
		echo $valu;
		echo '</td>';
}
}
}
	?>
	<td width="90">
    <?php
	 if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
	   $resultte=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   $Exc_que="SELECT * FROM competition WHERE School_Id='$Own_School_Id'";
   $number_of_rows=mysql_num_rows($resultte);
	    if($resultte)
  {
  if(mysql_num_rows($resultte)!=0)
  {
  $_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
  }
  }
  $n=mysql_num_rows($resultte);
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
	{
		$resultte=mysql_query("SELECT * FROM competition");
   $Exc_que="SELECT * FROM competition";
   $number_of_rows=mysql_num_rows($resultte);
  $n=mysql_num_rows($resultte);
	}
    echo "<div class='ctxt'><input type='text' size='5' name='$name' id='$groupcolumn'  onkeyup='GetCol(this.value,this.name,$rnum)' onclick='GetTBox($rnum,$n)'/></div>";
	$button="button".$rnum;
		echo "<div id='$button' class='sub_but'></div>";
	?></td>
    <?php
  echo '</tr>';
  }
  
   echo "</table>";
   if(mysql_num_rows($result)>=10)
   {
	 echo '</div>';
	 }
     $Column_names=explode(",",$_SESSION['Column_name']);
   if(count($Column_names)>0&&$_SESSION['Column_name']!="")
  {
   ?>
   <br/>
   <form action="#"/>
   <input type="submit" name="groupback" class="groupback" value=""/></form>
   <?php
   }
  ?></form><?php
}
	   }
	}
	}
		else
	{
	if(isset($_POST['groupback']))
  {
     $rnum=0;
	
  	 if($resultgro)
	 {	  
$qucol="SHOW COLUMNS FROM competition";
$resultcol=mysql_query($qucol);
$totcol=mysql_num_rows($resultcol);
while($rowcolu=mysql_fetch_row($resultcol))
{
if($rowcolu[0]!="Last_Updated_Date")
{
if(empty($colnamearray))
{
$colnamearray=$rowcolu[0];
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
}
}
$colnamearray=$colnamearray.",Last_Updated_Date";
$colnamearray=$colnamearray.",Competition Name";
if(mysql_num_rows($resultgro)>=10)
   {
	 echo '<div style="height:410px; overflow-Y:scroll; ">';
	 }
  echo '<table width="937" border="1" class="tablefontv">';
  echo '<tr bgcolor="#6AB5FF">';
		if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
echo '<td width="39"></td>
<td width="39"></td>';
}
$cnamearray=explode(',',$colnamearray);
     for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	$th_cname= str_replace("_"," ",$val);
	$valu=$row[$val];
        echo '<td>';
	if($val=="School_Id")
  {
    $cnu=$cv+1;
		echo "(".$cnu.")School Name";
  }
  else
  {
		  $cnu=$cv+1;
		echo "(".$cnu.")".$th_cname;
		}
		echo '</td>';
   
	}
	echo '<td width="159">Column No</td>';
	echo '</tr>';
while($row=mysql_fetch_array($resultgro))
  {
$rnum=$rnum+1;
$c=$row['Competition_Id'];
$d=$row['Date'];
$s=$row['School_Id'];
$delVerification="delVerification".$rnum;
$name=$c.",".$d.",".$s;
?>
<tr bgcolor="#999999"><?php echo "<form action='#' method='post'>";
   	if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	?>
<td width="39">
<a href='?action=edit&Competition_Id=<?php echo $c; ?>&Date=<?php echo $d; ?>&School_Id=<?php echo $s; ?>'><img src="../images/edit.png"/></a></td><td width="39"><?php echo "<input type='submit' id='$name' name='dele' onclick='DeleteVerification($rnum,this.id)' class='submitdelete' value=''/>";?></td>
	<?php 
	}
	$cnamearray=explode(',',$colnamearray);
	for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
      if($cv==9)
		{
$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$C_ID'");
  if($rownet=mysql_fetch_array($resultnet))
  {

    echo '<td>';
		echo $rownet['Competition_Name'];
		echo '</td>';
	
  }
  else
  {

    echo '<td>';
		echo '';
		echo '</td>';
	 
  }
		}
		else
		{
		if($val=="School_Id")
{
$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$valu."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
echo '<td>';
		echo $Name;
		echo '</td>';
}
else
{
            echo '<td>';
		echo $valu;
		echo '</td>';
    	}
		}
   
	}
	?>
	<td width="90">
	<?php
	if($_SESSION['Access_Type']=="Administrator")
	{
		$resultte=mysql_query("SELECT * FROM competition");
  $n=mysql_num_rows($resultte);
	}
	if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	 $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
	   $resultte=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   $Exc_que="SELECT * FROM competition WHERE School_Id='$Own_School_Id'";
   $number_of_rows=mysql_num_rows($resultte);
	    if($resultte)
  {
  if(mysql_num_rows($resultte)!=0)
  {
  $_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
  }
  }
  $n=mysql_num_rows($resultte);
	}
	}
    echo "<div class='ctxt'><input type='text' size='5' name='$name' id='$groupcolumn'  onkeyup='GetCol(this.value,this.name,$rnum)' onclick='GetTBox($rnum,$n)'/></div>";
	$button="button".$rnum;
		echo "<div id='$button' class='sub_but'></div>";
	?></td>
    <?php
  echo '</tr>';
  }
  }
   echo "</table>";
   if(mysql_num_rows($resultgro)>=10)
   {
	 echo '</div>';
	 }
   $Column_names=explode(",",$_SESSION['Column_name']);
   if(count($Column_names)>0&&$_SESSION['Column_name']!="")
  {
    ?>
   <br/>   
   <form action="#"/>
   <input type="submit" name="groupback" class="groupback" value=""/></form>
   <?php
   }
   echo "</form>";  
   }
   if(!isset($_POST['groupback']))
  {
	  if($_SESSION['Access_Type']=="Administrator")
{
$Exc_que="SELECT * FROM competition";
$result=mysql_query("SELECT * FROM competition");
   $number_of_rows=mysql_num_rows($result);
}
 else if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	 $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   $result=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   $Exc_que="SELECT * FROM competition WHERE School_Id='$Own_School_Id'";
   $number_of_rows=mysql_num_rows($result);
   }
   }
if($result)
   {
    if(mysql_num_rows($result)==0)
   {
   $_SESSION['errornorow']="Sorry there is no result to view!";
   }
   if(mysql_num_rows($result)>0)
   {
   $rnum=0;
$qucol="SHOW COLUMNS FROM competition";
$resultcol=mysql_query($qucol);
$totcol=mysql_num_rows($resultcol);
while($rowcolu=mysql_fetch_row($resultcol))
{
if($rowcolu[0]!="Last_Updated_Date")
{
if(empty($colnamearray))
{
$colnamearray=$rowcolu[0];
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
}
}
$colnamearray=$colnamearray.",Last_Updated_Date";
$colnamearray=$colnamearray.",Competition Name";

 if($action!="group")
  {
   if(mysql_num_rows($result)>=10)
   {
	 echo '<div style="height:410px; overflow-Y:scroll; ">';
	 }
   echo '<table width="937" border="1" class="tablefontv">';
  echo '<tr bgcolor="#6AB5FF">';
		if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
echo '<td width="39"></td>
<td width="39"></td>';
}
$cnamearray=explode(',',$colnamearray);
     for($cv=0;$cv<count($cnamearray);$cv++)
	{
	 $val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
  echo '<td>';
  if($val=="School_Id")
  {
    $cnu=$cv+1;
		echo "(".$cnu.")School Name";
  }
  else
  {
		  $cnu=$cv+1;
		echo "(".$cnu.")".$th_cname;
		}
		echo '</td>';
   
	}
	echo '<td width="159">Column No</td>';
	echo '</tr>';
  while($row=mysql_fetch_array($result))
  {
	  $rnum=$rnum+1;
$s=$row['School_Id'];
$c=$row['Competition_Id'];
$d=$row['Date'];
$delVerification="delVerification".$rnum;
$name=$c.",".$d.",".$s;
?>
<tr bgcolor="#999999"><?php echo "<form action='#' method='post'>";
   	if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	?>
<td width="39"><a href='?action=edit&Competition_Id=<?php echo $c; ?>&Date=<?php echo $d; ?>&School_Id=<?php echo $s; ?>'><img src="../images/edit.png"/></a></td>
<td width="39"><?php echo "<input type='submit' id='$name' name='dele' onclick='DeleteVerification($rnum,this.id)' class='submitdelete' value=''/>";?></td>
	<?php 
	}
	$cnamearray=explode(',',$colnamearray);
	for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		$valu=$row[$val];
			if($cv==9)
		{
$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$C_ID'");
  if($rownet=mysql_fetch_array($resultnet))
  {

    echo '<td>';
		echo $rownet['Competition_Name'];
		echo '</td>';
	
  }
  else
  {

    echo '<td>';
		echo '';
		echo '</td>';
	 
  }
		}
		else
		{
		if($val=="School_Id")
{
$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$valu."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
echo '<td>';
		echo $Name;
		echo '</td>';
}
else
{
            echo '<td>';
		echo $valu;
		echo '</td>';
    	}
		}
    	}

	?>
	<td width="159"><?php
	 if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	 $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
    	   $resultte=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   if($resultte)
  {
  if(mysql_num_rows($resultte)!=0)
  {
  $_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
  }
  }
  $n=mysql_num_rows($resultte);
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
	{
		$resultte=mysql_query("SELECT * FROM competition");
$n=mysql_num_rows($resultte);
	}
    echo "<div class='ctxt'><input type='text' size='5' name='$name' id='$groupcolumn'  onkeyup='GetCol(this.value,this.name,$rnum)' onclick='GetTBox($rnum,$n)'/></div>";
	$button="button".$rnum;
		echo "<div id='$button' class='sub_but'></div>";
	?></td>
   <?php
  echo '</tr>';
  }
  echo '</table>';
   if(mysql_num_rows($result)>=10)
   {
	 echo '</div>';
	 }
  }
   if($action=="group")
  {  
$l_val=count($Column_names)-1;
   $gval=explode(",",$_SESSION['gvals']); 
  	$cnamearray=explode(',',$colnamearray);
	if($_SESSION['Access_Type']=="Administrator")
  {  
      $res_que="SELECT * FROM competition WHERE ";
  for($j=0;$j<count($Column_names);$j++)
  {
  $Col_name=$Column_names[$j];
       $res_que=$res_que.$Col_name."='".$gval[$j]."'";
   if($j!=(count($Column_names)-1))
  {
  $res_que=$res_que." AND ";
  }
  }
   $Exc_que=$res_que;
	$resultgro=mysql_query($res_que);
   $number_of_rows=mysql_num_rows($resultgro);
  }
   if($gval[$l_val]!="")
  {
  if(mysql_num_rows($resultgro)>=10)
   {
 echo '<div style="height:410px; overflow-Y:scroll; ">';
 }
   echo '<table width="937" border="1" class="tablefontv">';
echo '<tr bgcolor="#6AB5FF">';
		if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
echo '<td width="39"></td>
<td width="39"></td>';
}
     for($cv=0;$cv<count($cnamearray);$cv++)
	{
	 $val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
  echo '<td>';
  if($val=="School_Id")
  {
    $cnu=$cv+1;
		echo "(".$cnu.")School Name";
  }
  else
  {
		  $cnu=$cv+1;
		echo "(".$cnu.")".$th_cname;
		}
		echo '</td>';
   
	}
	echo '<td width="159">Column No</td>';
	echo '</tr>';
  $Column_names=explode(",",$_SESSION['Column_name']);
    $gval=explode(",",$_SESSION['gvals']);
	if($_SESSION['Column_name']!="")
  {
  
 if($_SESSION['Access_Type']=="Teacher in Charge"||$_SESSION['Access_Type']=="Teacher")
  {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   }
      $res_que="SELECT * FROM competition WHERE ";
for($j=0;$j<count($Column_names);$j++)
  {
  $Col_name=$Column_names[$j];
     $res_que=$res_que.$Col_name."='".$gval[$j]."'";
  
  if($j!=(count($Column_names)-1))
  {
  $res_que=$res_que." AND ";
  }
  if($j==(count($Column_names)-1))
  {
  $res_que=$res_que." AND School_Id='".$Own_School_Id."'";
  }
  }
     $resultgro=mysql_query($res_que);
   $Exc_que=$resultgro;
   $number_of_rows=mysql_num_rows($resultgro);
 }
   while($row=mysql_fetch_array($resultgro))
  {
  $rnum=$rnum+1;    
$c=$row['Competition_Id'];
$d=$row['Date'];
$s=$row['School_Id'];
$delVerification="delVerification".$rnum;
$name=$c.",".$d.",".$s;
?>
<tr bgcolor="#999999"><?php echo "<form action='#' method='post'>";
   	if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	?>
<td width="39">
<a href='?action=edit&Competition_Id=<?php echo $c; ?>&Date=<?php echo $d; ?>&School_Id=<?php echo $s; ?>'><img src="../images/edit.png"/></a></td>
<td width="39"><?php echo "<input type='submit' id='$name' name='dele' onclick='DeleteVerification($rnum,this.id)' class='submitdelete' value=''/>";?></td>
	<?php 
	}
	$cnamearray=explode(',',$colnamearray);
	for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		$valu=$row[$val];
       if($cv==9)
		{
$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$C_ID'");
  if($rownet=mysql_fetch_array($resultnet))
  {

    echo '<td>';
		echo $rownet['Competition_Name'];
		echo '</td>';
	
  }
  else
  {

    echo '<td>';
		echo '';
		echo '</td>';
	 
  }
		}
		else
		{
		if($val=="School_Id")
{
$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$valu."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
echo '<td>';
		echo $Name;
		echo '</td>';
}
else
{
            echo '<td>';
		echo $valu;
		echo '</td>';
    	}
		}
	}
	?>
	<td width="90">
	<?php
	 if($_SESSION['Access_Type']=="Teacher in Charge"||$_SESSION['Access_Type']=="Teacher")
	{
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
	   $resultte=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
  if($resultte)
  {
  if(mysql_num_rows($resultte)!=0)
  {
  $_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
  }
  }
  $n=mysql_num_rows($resultte);
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
	{
		$resultte=mysql_query("SELECT * FROM competition");
$n=mysql_num_rows($resultte);
	}
    echo "<div class='ctxt'><input type='text' size='5' name='$name' id='$groupcolumn'  onkeyup='GetCol(this.value,this.name,$rnum)' onclick='GetTBox($rnum,$n)'/></div>";
	$button="button".$rnum;
		echo "<div id='$button' class='sub_but'></div>";
	?></td>
    <?php
  echo '</tr>';
  }
  }
  echo '</table>';
  if(mysql_num_rows($resultgro)>=10)
   {
  echo '</div>';
  }
  }
  else
  {
  if(mysql_num_rows($result)>=10)
   {
 echo '<div style="height:410px; overflow-Y:scroll; ">';
 }
   echo '<table width="937" border="1" class="tablefontv">';
   	echo '<tr bgcolor="#6AB5FF">';
		if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
echo '<td width="39"></td>
<td width="39"></td>';
}
$cnamearray=explode(',',$colnamearray);
     for($cv=0;$cv<count($cnamearray);$cv++)
	{
	 $val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		
	$valu=$row[$val];
  echo '<td>';
  if($val=="School_Id")
  {
    $cnu=$cv+1;
		echo "(".$cnu.")School Name";
  }
  else
  {
		  $cnu=$cv+1;
		echo "(".$cnu.")".$th_cname;
		}
		echo '</td>';
   
	}
	echo '<td width="159">Column No</td>';
	echo '</tr>';
  while($row=mysql_fetch_array($result))
  {
	  $rnum=$rnum+1;
$s=$row['School_Id'];
$c=$row['Competition_Id'];
$d=$row['Date'];
$delVerification="delVerification".$rnum;
$name=$c.",".$d.",".$s;
?>
<tr bgcolor="#999999"><?php echo "<form action='#' method='post'>";
   	if($_SESSION['Access_Type']=="Administrator"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	?>
<td width="39">
<a href='?action=edit&Competition_Id=<?php echo $c; ?>&Date=<?php echo $d; ?>&School_Id=<?php echo $s; ?>'><img src="../images/edit.png"/></a></td>
<td width="39"><?php echo "<input type='submit' id='$name' name='dele' onclick='DeleteVerification($rnum,this.id)' class='submitdelete' value=''/>";?></td>
	<?php 
		}
	$cnamearray=explode(',',$colnamearray);
	for($cv=0;$cv<count($cnamearray);$cv++)
	{
	$val=$cnamearray[$cv];
	if($val=="Competition_Id")
	{
	$C_ID=$row[$val];
	}
	  $th_cname= str_replace("_"," ",$val);
		$valu=$row[$val];
			if($cv==9)
		{
$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$C_ID'");
  if($rownet=mysql_fetch_array($resultnet))
  {

    echo '<td>';
		echo $rownet['Competition_Name'];
		echo '</td>';
	
  }
  else
  {

    echo '<td>';
		echo '';
		echo '</td>';
	 
  }
		}
		else
		{
		if($val=="School_Id")
{
$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$valu."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
echo '<td>';
		echo $Name;
		echo '</td>';
}
else
{
            echo '<td>';
		echo $valu;
		echo '</td>';
    	}
    	}
}
	?>
	<td width="159"><?php
	 if($_SESSION['Access_Type']=="Teacher"||$_SESSION['Access_Type']=="Teacher in Charge")
	{
	 $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
    	   $resultte=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
   if($resultte)
  {
  if(mysql_num_rows($resultte)!=0)
  {
  $_SESSION['errornotallo']="Sorry you are not allowed to view all details!";
  }
  }
  $n=mysql_num_rows($resultte);
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
	{
		$resultte=mysql_query("SELECT * FROM competition");
$n=mysql_num_rows($resultte);
	}
    echo "<div class='ctxt'><input type='text' size='5' name='$name' id='$groupcolumn'  onkeyup='GetCol(this.value,this.name,$rnum)' onclick='GetTBox($rnum,$n)'/></div>";
	$button="button".$rnum;
		echo "<div id='$button' class='sub_but'></div>";
	?></td>
   <?php
  echo '</tr>';
  }
  echo '</table>';
  if(mysql_num_rows($result)>=10)
   {
  echo '</div>';
  }
  }
  }
   $Column_names=explode(",",$_SESSION['Column_name']);
   if(count($Column_names)>0&&$_SESSION['Column_name']!="")
  {
   ?>
   <br/>
   <form action="#"/>
   <input type="submit" name="groupback" class="groupback" value=""/></form>
   <?php
   }
   echo "</form>";
}
}
 else
	   {
	   $_SESSION['errornorow']="Sorry there is no result to view!";
	   }
	   }
}
if($_GET['action']!="edit"&&$action!="delete")
  {
  if($number_of_rows>0)
   {
$Exc_quer=str_replace("'","@",$Exc_que);
 echo "<form action='../exportreport.php' method='post'><input type='submit' name='ToExcel' class='print' value=''/><input type='hidden' value='$Exc_quer' name='query'/><input type='hidden' value='competition' name='Exc_table_name'/></form>";
  }
  }
  ?>
<font class="err">
<?php
 if(isset($_SESSION['errornotallo']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['errornotallo'];
unset($_SESSION['errornotallo']);
}
if(isset($_SESSION['errornorow']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['errornorow'];
unset($_SESSION['errornorow']);
}
if(isset($_SESSION['value_empty']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['value_empty'];
unset($_SESSION['value_empty']);
}
if(isset($_SESSION['groupbyempty']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['groupbyempty'];
unset($_SESSION['groupbyempty']);
}
	if(isset($_POST['Go']))
	 {
	if(isset($_SESSION['up']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['up'];
unset($_SESSION['up']);
}
}
if($action=="delete")
{
	if(isset($_SESSION['del']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['del'];
unset($_SESSION['del']);
}
}
	?>
	</font>
	  </div>
  </div>
</div>
<div id="sidebar">
<br/>
<div id="partners" class="boxed">
			<h2 class="title">COMPETITIONS</h2>
            </div>
            <?php
include_once "menu.php";
?>
 <?php
include_once "../footer.php";
}
?>

</body>
</html>